package com.ruoyi.supplier.controller;

import java.util.List;

import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.supplier.domain.TbPurchaseDTO;
import com.ruoyi.supplier.domain.TbPurchaseDetail;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TbPurchase;
import com.ruoyi.supplier.service.ITbPurchaseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购Controller
 * 
 * @author ruoyi
 * @date 2021-02-16
 */
@RestController
@RequestMapping("/supplier/purchase")
public class TbPurchaseController extends BaseController
{
    @Autowired
    private ITbPurchaseService tbPurchaseService;

    /**
     * 查询采购列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbPurchase tbPurchase)
    {
        startPage();
        List<TbPurchase> list = tbPurchaseService.selectTbPurchaseList(tbPurchase);
        return getDataTable(list);
    }


    /**
     * 查询采购明细列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:list')")
    @GetMapping("/listDetail")
    public TableDataInfo listDetail(TbPurchaseDTO tbPurchaseDTO)
    {
        startPage();
        List<TbPurchaseDTO> list = tbPurchaseService.selectTbPurchaseDetailList(tbPurchaseDTO);
        return getDataTable(list);
    }

    /**
     * 导出采购列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:export')")
    @Log(title = "采购", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbPurchase tbPurchase)
    {
        List<TbPurchase> list = tbPurchaseService.selectTbPurchaseList(tbPurchase);
        ExcelUtil<TbPurchase> util = new ExcelUtil<TbPurchase>(TbPurchase.class);
        return util.exportExcel(list, "purchase");
    }

    /**
     * 获取采购详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbPurchaseService.selectTbPurchaseById(id));
    }

    /**
     * 新增采购
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:add')")
    @Log(title = "采购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbPurchase tbPurchase)
    {
        return toAjax(tbPurchaseService.insertTbPurchase(tbPurchase));
    }

    /**
     * 修改采购
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:edit')")
    @Log(title = "采购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbPurchase tbPurchase)
    {
        return toAjax(tbPurchaseService.updateTbPurchase(tbPurchase));
    }

    /**
     * 删除采购
     */
    @PreAuthorize("@ss.hasPermi('supplier:purchase:remove')")
    @Log(title = "采购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        int i =tbPurchaseService.deleteTbPurchaseByIds(ids);
        if(i==-1) {
            return new AjaxResult(HttpStatus.SUCCESS, "包含已提交数据，不能删除", -1);
        }
        return toAjax(i);
    }
}
